#include <iostream>
#include <vector>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;


vector <int> G[100005];
int n,m;
bool vis[100005];

void dfs(int x){
	
	cout<<x<<" ";
	
	for(int i=0;i<G[x].size();i++){
		if(!vis[G[x][i]]){
			vis[G[x][i]]=true;
			dfs(G[x][i]);
		}
	}
}

void bfs(){
	memset(vis,false,sizeof(vis));
	queue <int> Q;
	vis[1]=true;
	Q.push(1);
	while(!Q.empty()){
		int cur=Q.front();
		cout<<cur<<" ";
		Q.pop();
		
		for(int i=0;i<G[cur].size();i++){
			if(!vis[G[cur][i]]){
				Q.push(G[cur][i]);
				vis[G[cur][i]]=true;
			}
		}
		 
	}
}
int main(){
	int u,v;
	cin>>n>>m;
	for(int i=1;i<=m;i++){
		cin>>u>>v;
		G[u].push_back(v);
	}
	for(int i=1;i<=n;i++){
		sort(G[i].begin(),G[i].end());
	}
	memset(vis,false,sizeof(vis));
	vis[1]=true;
	dfs(1);
	cout<<endl;
	bfs();
	return 0;
}
